<?php

namespace app\common\model;

use think\Model;

class OfflineInformation extends Model
{
    // 表名,不含前缀
    protected $name = 'offline_information';
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    protected $type = [
        'createtime' => 'datetime:Y-m-d H:i:s',
        'updatetime' => 'datetime:Y-m-d H:i:s',
    ];

    /**
     * @var null|static 实例对象
     */
    protected static $instance = null;

    /**
     * 获取示例
     * @param array $options 实例配置
     * @return static
     */
    public static function instance($options = [])
    {
        if (is_null(self::$instance)) self::$instance = new self($options);

        return self::$instance;
    }

    public function _initialize($options = [])
    {
        $this->data = $options;
        return $this;
    }

    protected function getTopicAttr($value)
    {
        return $value ? explode(',', $value) : '';
    }

    protected function getIndustryAttr($value)
    {
        return $value ? explode(',', $value) : '';
    }

    protected function setTopicAttr($value)
    {
        if (is_string($value)) {
            return $value;
        } else {
            return $value ? implode(',', $value) : '';
        }
    }

    protected function setIndustryAttr($value)
    {
        if (is_string($value)) {
            return $value;
        } else {
            return $value ? implode(',', $value) : '';
        }
    }

    protected function setAddressAttr($value)
    {
        if (is_string($value)) {
            return $value;
        } else {
            return $value ? implode(',', $value) : '';
        }
    }

    public function delField(string $field = '')
    {
        if ($field) {
            $field = explode(',', $field);
            foreach ($field as $fieldKey => $fieldValue) {
                unset($this->data[trim($fieldValue)]);
            }
        }
        return $this->data;
    }

    /**
     * @param array $params
     * @param int $uid
     * @param int $page
     * @param int $pageSize
     * @return mixed
     * @throws
     * */
    public function getMyTeacher(array $params, int $uid, int $page, int $pageSize): object
    {
        $condition['t1.user_id'] = $uid;
        if (!empty($params['address'])) {
            $condition['t1.address'] = ['LIKE', '%' . $params['address'] . '%'];
        }
        if (!empty($params['industry'])) {
            $condition['t1.industry'] = ['LIKE', '%' . $params['industry'] . '%'];
        }
        if (!empty($params['topic'])) {
            $condition['t1.topic'] = ['LIKE', '%' . $params['topic'] . '%'];
        }
        if (!empty($params['searchName'])) {
            $condition['t1.username|t1.mobile|t1.wxh|t1.introduce|t1.topic|t1.industry|t1.note|t3.name'] = ['LIKE', '%' . $params['searchName'] . '%'];
        }
        $order = 't1.price desc';
        if (!empty($params['price'])) {
            if ($params['price'] == 'asc') {
                $order = 't1.price asc';
            } else {
                $order = 't1.price desc';
            }
        }
        return $this->alias('t1')
            ->field('t1.id, t1.username as name, t2.nickname, t1.address as city, t1.topic, t1.industry, t1.note, t1.introduce, t1.status, t1.user_id as bind_uid, t1.to_user_id, t1.state')
            ->field('if(t1.price > 0, t1.price, t2.price) as price, t1.mobile, t1.wxh as wechat_number, t2.is_rz, t2.status as user_info_status')
            ->field('t1.type, t1.content_type')
            ->join('user_info t2', 't1.to_user_id = t2.user_id', 'LEFT')
            ->join('information_qualifications t3', 't3.oid = t1.id', 'LEFT')
            ->where($condition)
            ->order($order)
            ->group('t1.id')
            ->paginate($pageSize, false, ['page' => $page])->each(function (&$value) use ($uid, $params) {
                $value['user_info_status'] = empty($value['user_info_status']) ? 0 : $value['user_info_status'];
                if ($value['content_type'] == 1) {
                    $value['qualifications'] = UserQualifications::instance()->where(['user_id' => $value['to_user_id'], 'status' => 2])->select();
                    $value['content'] = Content::instance()->where(['user_id' => $value['to_user_id'], 'status' => 2])->select();
                    $value['is_rz'] = ($value['is_rz'] == 2) ? true : false;
                } else {
                    $value['qualifications'] = InformationQualifications::instance()->where(['oid' => $value['id'], 'status' => 2])->select();
                    $value['is_rz'] = false;
                }
                if (!empty($params['searchName'])) {
                    // 使用 preg_replace 高亮关键词
                    $value['highlightedName'] = preg_replace(
                        '/(' . preg_quote($params['searchName'], '/') . ')/i',
                        '<span class="highlight">$1</span>',
                        $value['name']
                    );
                }
                /*if (!empty($value['user_id'])) {
                    $value['qualifications'] = UserQualifications::instance()->field('id, name, path, path_file_names')
                        ->where(['user_id' => $value['user_id'], 'status' => 2])
                        ->select();
                }*/
                // 获取是否存在审核的数据
                $showExamine = collection(ShowExamine::instance()->where(['user_id' => $uid, 'type' => 3])->select())->toArray();
                foreach ($showExamine as $keys => $values) {
                    if (!empty($values['new_date'])) {
                        $new_date = array_column(json_decode($values['new_date'], true), 'id');
                        $value['is_sh'] = in_array($value['id'], $new_date) ? $values['status'] : 0;
                    }
                }
            });
    }
}